/**
 * Copyright (C), 2015-2020, XXX有限公司
 * FileName: ExcelController
 * Author:   zmjha
 * Date:     2020/8/2 11:12
 * Description: Excel 导入导出 web层
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.zmj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zmj.entity.WeiUser;
import com.zmj.service.ExcelService;
import com.zmj.utils.CreateBarDataUtil;
import com.zmj.utils.FileUtil;
import com.zmj.utils.JsonResult;
import com.zmj.utils.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br> 
 * 〈Excel 导入导出 web层〉
 *
 * @author zmjha
 * @create 2020/8/2
 * @since 1.0.0
 */

@Controller
public class ExcelController {

    @Autowired
    ExcelService excelService;

    @RequestMapping("/addExcel")
    @ResponseBody
    public Message addExcel(MultipartFile fileExcel ,HttpServletRequest request) throws IOException {

      /* String path=request.getSession().getServletContext().getRealPath("excel");
        System.out.println(path);
        File file=new File(path);
        if (!file.exists()){
            file.mkdir();
        }
*/
        String fileName=fileExcel.getOriginalFilename();
        /*if (!fileExcel.isEmpty()){
            fileExcel.transferTo(new File(file,fileName));
        }*/

       /* List<WeiUser> weiUserList = FileUtil.importExcel(path + File.separator + fileName, 0, 1, WeiUser.class);*/

       /* String path= "E://feiq//Recv Files//2020-8-1上机测试//weiuser.xlsx";
       List<WeiUser> weiUserList = FileUtil.importExcel(path, 0, 1, WeiUser.class);*/

       List<WeiUser> weiUserList=FileUtil.importExcel(fileExcel,0,1,WeiUser.class);

       boolean flg= excelService.addExcel(weiUserList);
       Message message=new Message();
       if (flg){
           message.setCode(1);
           message.setMessage("导入成功");
       }else {
           message.setCode(0);
           message.setMessage("导入失败");
       }
        return message;
    }

    @RequestMapping("/tomain")
    public String tomain(Map<String,Object> map,String pageNum){

        if (pageNum==null){
            pageNum="1";
        }
        int num=Integer.parseInt(pageNum);
        PageHelper.startPage(num,10);
        List<WeiUser> weiUserList=excelService.exportExcel();
        PageInfo pageInfo=new PageInfo(weiUserList);
        map.put("pageInfo",pageInfo);
        return "main";
    }


    @RequestMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
       List<WeiUser> weiUserList= excelService.exportExcel();
       FileUtil.exportExcel(weiUserList,"微信用户表","sheet1",WeiUser.class,"weiuser",response);
    }

    @RequestMapping("/table")
    @ResponseBody
    public JsonResult toTable(){
        int count =excelService.selCount();
        List<WeiUser> weiUserList=excelService.exportExcel();
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode(0);
        jsonResult.setCount(count);
        jsonResult.setData(weiUserList);
        jsonResult.setMsg("表格数据");
        return jsonResult;
    }

    @RequestMapping("/delOne")
    @ResponseBody
    public Message delOne(Integer id){
        boolean flg=excelService.delOne(id);
        Message message=new Message();
        if (flg){
            message.setCode(1);
            message.setMessage("删除成功");
        }else {
            message.setCode(0);
            message.setMessage("删除失败");
        }
        return  message;
    }

    @RequestMapping("/delAll")
    @ResponseBody
    public Message delAll(@RequestParam List<Integer> ids){
        boolean flg=excelService.delAll(ids);
        Message message=new Message();
        if (flg){
            message.setCode(1);
            message.setMessage("删除选中成功");
        }else {
            message.setCode(0);
            message.setMessage("删除选中失败");
        }
        return  message;
    }

    @RequestMapping("/selExcel")
    public String selExcel(WeiUser user,String pageNum,Map<String,Object> map){

        List<String> citys= excelService.selCity();
        List<String> countrys= excelService.selCountry();
        List<String> provinces= excelService.selProvince();
        map.put("citys",citys);
        map.put("countrys",countrys);
        map.put("provinces",provinces);
        if (pageNum==null){
            pageNum="1";
        }
        int num=Integer.parseInt(pageNum);
        PageHelper.startPage(num,5);
        List<WeiUser> weiUserList= excelService.selExcel(user);
        PageInfo pageInfo=new PageInfo(weiUserList);
        map.put("pageInfo",pageInfo);

        return "select";
    }

    @RequestMapping("/enchart")
    @ResponseBody
    public Map<String, Object> selCountCountry() throws Exception {
        System.out.println(CreateBarDataUtil.getBarDatas(excelService.selCountCountry()));
       return CreateBarDataUtil.getBarDatas(excelService.selCountCountry()) ;

    }

    @RequestMapping("toenchart")
    public String tochart(){
        return "enchart";
    }
}